package AkkaDemos

import akka.actor.{Actor, ActorRef, ActorSystem, Props}


object ActortExample {
  def main(args: Array[String]): Unit = {
    //创建一个actorSystemt,用于管理所有的actor
    val actoryFactory = ActorSystem("actoryFactory")
    //创建一个actor的同时,返回actory的actorRef
    //    val bacf = actoryFactory.actorOf(Props[BActor], "bac")
    //    val aacf = actoryFactory.actorOf(Props(new AActor(bacf)), "aac")
    val ref = actoryFactory.actorOf(Props.apply(classOf[AActor]))
  }

  class AActor(receiver: ActorRef) extends Actor {
    val receiverRef: ActorRef = receiver

    //这个方法会被管理该actor的MailBox调用
    //每个actor都会有一个MailBox,MailBox收到消息就会调用
    override def receive: Receive = {
      case "hello" => {
        println("收到hello,回应hello too")
      }
      case "ok" => println("收到hello,回应ok")
      case "exit" => {
        println("收到停止模型指令")
        context.stop(self) //停止actorRef
        context.system.terminate() //推出actorFactory
      }

      case "response" => {
        println("好的,我知道了")
        receiverRef ! ("over")
      }
      case _ => println("匹配不到")

    }
  }

  class BActor(receiver: ActorRef) extends Actor {
    val receiverRef: ActorRef = receiver

    override def receive: Receive = {
      case "over" => {
        println("say hello 结束")
      }
      case "aaa" => {
        println("Hi")
        sender() ! ("ok")
      }
      case _ =>
    }
  }

}